package com.xiaoma.tpo.service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.ClsSentenceDao;
import com.xiaoma.tpo.dao.ClsWordDao;
import com.xiaoma.tpo.dao.WdGroupDao;
import com.xiaoma.tpo.dao.WdPlanDao;
import com.xiaoma.tpo.dao.WdScoreDao;
import com.xiaoma.tpo.dao.WdUnlockProcessDao;
import com.xiaoma.tpo.dao.WdWordDao;
import com.xiaoma.tpo.domain.ClsSentence;
import com.xiaoma.tpo.domain.ClsWord;
import com.xiaoma.tpo.domain.WdGroup;
import com.xiaoma.tpo.domain.WdScore;
import com.xiaoma.tpo.domain.WdUnlockProcess;
import com.xiaoma.tpo.domain.WdWord;
import com.xiaoma.tpo.dto.WdGroupVo;
import com.xiaoma.tpo.dto.WdPlanVo;
import com.xiaoma.tpo.utils.Pager;

@Service
public class WordService {

    @Autowired
    private WdPlanDao planDao;
    
    @Autowired
    private WdGroupDao groupDao;
    
    @Autowired
    private WdWordDao wordDao;
    
    @Autowired
    private WdUnlockProcessDao unlockProcessDao;
    
    @Autowired
    private WdScoreDao scoreDao;
    
    @Autowired
    private ClsWordDao clsWordDao;
    @Autowired
    private ClsSentenceDao clsSentenceDao;
    
    public List<WdPlanVo> findAllWordPlans() {
        return planDao.findAllWordPlans();
    }
    
    
    public List<WdGroupVo> findWordGroupsByPlanId(int wordPlanId) {
        
        return groupDao.findWordGroupsByPlanId(wordPlanId);
    }
    
    public WdGroup findGroupById(int groupId) {
        return groupDao.getById(groupId);
    }
    
    
    public List<WdWord> findWordByGroupId(int groupId) {
        return wordDao.findByGroupId(groupId);
    }
    
    public WdUnlockProcess findProcessByPlanIdAndUserId(int planId, int userId) {
        WdUnlockProcess entity = new WdUnlockProcess();
        entity.setUserId(userId);
        entity.setWdPlanId(planId);
        List<WdUnlockProcess> pss = unlockProcessDao.findByColumns(entity);
        return pss.size() > 0 ? pss.get(0) : null;
    }
    
    public List<WdUnlockProcess> findProcessByUserId(int userId) {
        WdUnlockProcess entity = new WdUnlockProcess();
        entity.setUserId(userId);
        List<WdUnlockProcess> pss = unlockProcessDao.findByColumns(entity);
        return pss;
    }
    
    public void updateUnlockProcess(WdUnlockProcess process) {
        unlockProcessDao.update(process);
    }
    
    public void destroyUnlockProcess(int unlockProcessId) {
        unlockProcessDao.destroy(unlockProcessId);
    }
    
    public void saveUnlockProcess(WdUnlockProcess process) {
        unlockProcessDao.insert(process);
    }
    
    public WdScore findScoreByPlanIdAndGroupIdAndUserId(int planId, int groupId, int userId) {
        WdScore entity = new WdScore();
        entity.setWdPlanId(planId);
        entity.setWdGroupId(groupId);
        entity.setUserId(userId);
        List<WdScore> scores = scoreDao.findByColumns(entity);
        return scores.size() > 0 ? scores.get(0) : null;
        
    }
    
    public void saveScore(WdScore score) {
        scoreDao.insert(score);
    }
    
    public void updateScore(WdScore score) {
        scoreDao.update(score);
    }
    
    public int getRanking(WdScore wdScore, int planId, int groupId) {
        
        List<WdScore> scores = scoreDao.orderByScore(planId, groupId);
        int ranking = 1;
        for (WdScore item : scores) {
            if(item.getScore() == wdScore.getScore()){
                break;
            }
            ranking++;
        }
        return ranking;
        
    }


	public Pager findAllWords(Integer page,Integer rows,Integer clsGateId,String code,String word,String wordCn) {
		try {
			if(clsGateId != null){
				String clsGateId1 = URLDecoder.decode(clsGateId.toString(), "UTF-8");
				clsGateId = Integer.parseInt(clsGateId1);
			}
			if(code != null && code.length() >0)
				code = URLDecoder.decode(code, "UTF-8");
			if(word != null && word.length() >0)
				word = URLDecoder.decode(word, "UTF-8");
			if(wordCn != null && wordCn.length() >0)
				wordCn = URLDecoder.decode(wordCn, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Pager pager = clsWordDao.findWithCondition(page,rows,clsGateId,code,word,wordCn);
		return pager;
	}


	public void update(ClsWord clsWord) {
		clsWordDao.update(clsWord);
	}
	
}
